package com.bit.book.Controller;

import ch.qos.logback.core.util.StringUtil;
import com.bit.book.model.UserInfo;
import com.bit.book.service.UserInfoService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController

public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping("/login")
    public Boolean login(String name , String password , HttpSession session){
        //1. 参数校验
        if(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)){
            return false;
        }

        UserInfo userInfo = userInfoService.queryUserByName(name);
        // 如果用户输入的 username 为 null, 直接判断登录失败
        if(userInfo==null){
            return false;
        }

        if(password.equals(userInfo.getPassword())){
            userInfo.setPassword("");
            session.setAttribute("Constants.SESSION_USER_KEY", userInfo);
            return true;
        }
        return false;
    }
}
